package com.yiboshi.rehrs.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yiboshi.rehrs.domain.entity.ZhongOrder;
import com.yiboshi.rehrs.domain.mapper.ZhongOrderMapper;
import com.yiboshi.rehrs.domain.service.IZhongOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiboshi.rehrs.util.StrUtils;
import com.yiboshi.rehrs.util.ThreadPoolUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author local
 * @since 2025-07-08
 */
@Service
public class ZhongOrderServiceImpl extends AperBaseServiceImpl<ZhongOrderMapper, ZhongOrder> implements IZhongOrderService {

    @Override
    public ZhongOrder getHoldOne(String productId, Integer vipDays,String payAccount) {
        LambdaQueryWrapper<ZhongOrder> orderLambdaQueryWrapper = getQueryWrapper().eq(ZhongOrder::getProductId, productId)
                .eq(ZhongOrder::getVipDays, vipDays)
                .eq(ZhongOrder::getPayStatus, 0)
                .eq(ZhongOrder::getPayAccount, payAccount)
                /*.and(wrapper ->
                        wrapper
                                .or().isNull(ZhongOrder::getPayAccount)
                )*/
                .ge(ZhongOrder::getCreateTime, LocalDateTime.now().plusHours(-15))
                .orderByAsc(ZhongOrder::getId)
                .last("limit 1");
        ZhongOrder one = getOne(orderLambdaQueryWrapper);
        if(one==null){
            return null;
        }
        if(StrUtils.isNotBlank(one.getPayAccount())){
            ThreadPoolUtils.submitTask(() -> {
                one.setUpdateTime(LocalDateTime.now());
                updateById(one);
            });
            return one;
        }
        one.setPayAccount(payAccount);
        LambdaUpdateWrapper<ZhongOrder> updateWrapper = getUpdateWrapper()
                .eq(ZhongOrder::getId, one.getId())
                .eq(ZhongOrder::getPayStatus, 0)
                .isNull(ZhongOrder::getPayAccount)
                .set(ZhongOrder::getUpdateTime,LocalDateTime.now() )
                .set(ZhongOrder::getPayAccount, payAccount);
        boolean update = update(updateWrapper);
        if(update){
            return  one;
        }else{
            return null;
        }

    }

    @Override
    public List<ZhongOrder> listByJob() {
        LambdaQueryWrapper<ZhongOrder> listQuery = getQueryWrapper()
                .isNotNull(ZhongOrder::getPayAccount)
                .eq(ZhongOrder::getPayStatus, 0)
                .ge(ZhongOrder::getUpdateTime, LocalDateTime.now().plusMinutes(-13))//>= 10分钟以内
                .le(ZhongOrder::getUpdateTime, LocalDateTime.now().plusMinutes(-1))//<=
                .le(ZhongOrder::getCheckCount, 10);
        return list(listQuery);
    }

    @Override
    public void updateByJob() {
        /*LambdaUpdateWrapper<ZhongOrder> orderLambdaUpdateWrapper = getUpdateWrapper()
                .eq(ZhongOrder::getPayStatus, 0)
                .le(ZhongOrder::getUpdateTime, LocalDateTime.now().plusMinutes(-10)) //<= 10分钟以前 需要释放订单
                .set(ZhongOrder::getPayAccount, null)
                .set(ZhongOrder::getUpdateTime, LocalDateTime.now())
                .set(ZhongOrder::getCheckCount, 0);
        this.update(orderLambdaUpdateWrapper)*/


        ;
    }
}
